package org.marsson.bNav;



import gnu.io.CommPortIdentifier;

import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * BattleShip.java	Creates an easy TCP connection to another Connection Object
 *
 * @author       Marcelo Marsson
 * @version      1.0     March 3, 2010
 */
public class BattleShip extends JFrame
{
	private 	JMenuItem newGame, newServerGame, newClientGame, help, quit, about,
				 restart, internet;

	private JMenuBar bar;
	private JMenu fileMenu, optionMenu, challenge;
	private JCheckBoxMenuItem easy, normal, impossible;
	private static JCheckBoxMenuItem sound;
	private JPanel statusBar;
	private JLabel status;
	private Container c;
	public Game game;
	private boolean isServer = false;
	public SerialComm serialComm;
	public String port;
	private JPanel 	buttonPanel = new JPanel();

	public BattleShip()
	{
		super("BattleShip");
		c = getContentPane();

		ItemHandler itemHandler = new ItemHandler(this);

		setJMenuBar(bar = new JMenuBar());
		
		//Cria os Itens de Menu do Menu Arquivo:
		(fileMenu = new JMenu("Arquivo")).setMnemonic('F');		// This is the File Menu
		
		newServerGame = new JMenuItem("Novo jogo Servidor",'S');
		newServerGame.addActionListener(itemHandler);
		fileMenu.add(newServerGame);

		newClientGame = new JMenuItem("Novo jogo Cliente",'C');
		newClientGame.addActionListener(itemHandler);
		fileMenu.add(newClientGame);

		newGame = new JMenuItem("Novo Jogo Teste...",'G');
		newGame.addActionListener(itemHandler);
		fileMenu.add(newGame);

		restart = new JMenuItem("Reiniciar",'R');
		restart.addActionListener(itemHandler);
		restart.setEnabled(false);
		fileMenu.add(restart);

		//Cria os itens de menu do menu Sobre...
		fileMenu.addSeparator();
		about = new JMenuItem("Sobre...",'A');
		about.addActionListener(itemHandler);
		fileMenu.add(about);

		quit = new JMenuItem("Exit",'x');
		quit.addActionListener(itemHandler);
		fileMenu.add(quit);

		//Cria os itens do menu Opcoes
		(optionMenu = new JMenu("Opcoes")).setMnemonic('O');	 // This is the Console Menu

		help = new JMenuItem("Ajuda",'H');
		help.addActionListener(itemHandler);
		optionMenu.add(help);

		optionMenu.addSeparator();
		internet = new JMenuItem("Configuracao da porta serial...",'I');
		internet.addActionListener(itemHandler);
		optionMenu.add(internet);

		challenge = new JMenu("Auto Jogar");
		(easy = new JCheckBoxMenuItem("ligar")).addActionListener(itemHandler);
		challenge.add(easy);
		optionMenu.add(challenge);

		sound = new JCheckBoxMenuItem("Sound");
		sound.addActionListener(itemHandler);
		sound.setSelected(true);
		optionMenu.add(sound);

		bar.add(fileMenu);
		bar.add(optionMenu);

		//theDemo = new Demo();

		statusBar = new JPanel();
		status = new JLabel(" Location: " + Connection.getMyIP() + "     Port: " );
		statusBar.add(status);

		//c.add(theDemo, BorderLayout.CENTER);
		c.add(statusBar, BorderLayout.SOUTH);

		pack();
		setVisible(true);

		addWindowListener( new WindowAdapter()
			{ public void windowClosing( WindowEvent e )	{  System.exit(0);	}	}
		);
	}
	// Codigo para lidar com os eventos dos menus.
	private class ItemHandler implements ActionListener
	{
		private BattleShip battleship;
		
		ItemHandler(BattleShip battleship){
			this.battleship = battleship;
		}
		public void actionPerformed( ActionEvent e )
		{
			if (e.getSource()==newServerGame)
			{
				game = new ServerGame(battleship);
				c.add(game, BorderLayout.CENTER);
				//desktop.add(battleWindow);
				isServer = true;
				newServerGame.setEnabled(false);
				newClientGame.setEnabled(false);
				restart.setEnabled(true);
				pack();
				setVisible(true);
			}
			if (e.getSource()==newClientGame)
			{
				//desktop.add(battleWindow);
				game = new ClientGame(battleship);
				c.add(game, BorderLayout.CENTER);
				isServer = false;
				newServerGame.setEnabled(false);
				newClientGame.setEnabled(false);
				restart.setEnabled(true);
				pack();
				setVisible(true);
			}
			if (e.getSource()==newGame)
			{
				game = new Demo(battleship);
				c.add(game, BorderLayout.CENTER);
				isServer = false;
				newServerGame.setEnabled(false);
				newClientGame.setEnabled(false);
				restart.setEnabled(true);
				pack();
				setVisible(true);
			}
			if (e.getSource()==restart)
			{
				newServerGame.setEnabled(true);
				newClientGame.setEnabled(true);
			}
			if (e.getSource()==about) {
				JOptionPane.showMessageDialog(c,
					"Batalha Naval\n\n" +
					"Program Design:        William Dubel\n" +
					"Board Design:          Robert Ledesma\n" +
					"Game Logic:            Marcelo Marsson\n\n" +
					"Created: June 28, 2001",
					"About BattleShip", JOptionPane.PLAIN_MESSAGE);
			}
			if (e.getSource()==quit)	{  System.exit(0);	}


			
			if (e.getSource()==help)
			{
				new HelpBrowser();
			}
			if (e.getSource()==internet)
			{
				try
				{
					PortTest test = new PortTest();
					//CommPortIdentifier[] possibilities = test.getPorts();
					Object list[] =test.getStrings();
					String s =  (String) JOptionPane.showInputDialog(
							c.getParent(),
					                    "Selecione a Porta Serial:"
					                    ,
					                    "Selecao de portas:",
					                    JOptionPane.PLAIN_MESSAGE,
					                    null,
					                    test.getStrings(),
					                    test);
					
					//port = test.getPorts(s);
					serialComm = new SerialComm(s);
					port=s;

				}
				catch(NumberFormatException nfe)	{	/* do nothing	*/	}
				finally	{
					status.setText(" Location: " + Connection.getMyIP() + "     Port: " +port );
				}
			}
			if (e.getSource()==easy)
			{
				AutoShipFiller as = new AutoShipFiller(game.myField);
				new Thread(as).start();
				
			}
		}
	}

	public static boolean soundOn()		{	return sound.isSelected();	}

	public static void main(String [] args)
	{
		new BattleShip();		//Main creates a new instance of our BattleShip class
	}
}
